#include <stdio.h>
#include <Windows.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>

#define ROW 22 //锟斤拷戏锟斤拷锟斤拷锟斤拷
#define COL 42 //锟斤拷戏锟斤拷锟斤拷锟斤拷

#define KONG 0 //锟斤拷强眨锟绞裁匆裁伙拷校锟�
#define WALL 1 //锟斤拷锟角�
#define FOOD 2 //锟斤拷锟绞筹拷锟�
#define HEAD 3 //锟斤拷锟斤拷锟酵�
#define BODY 4 //锟斤拷锟斤拷锟斤拷锟�

#define UP 72 //锟斤拷锟斤拷锟斤拷锟斤拷锟�
#define DOWN 80 //锟斤拷锟斤拷锟斤拷锟斤拷锟�
#define LEFT 75 //锟斤拷锟斤拷锟斤拷锟斤拷锟�
#define RIGHT 77 //锟斤拷锟斤拷锟斤拷锟斤拷锟�
#define SPACE 32 //锟斤拷停
#define ESC 27 //锟剿筹拷

//锟斤拷头
struct Snake
{
    int len; //锟斤拷录锟斤拷锟斤拷锟斤拷锟斤拷
    int x; //锟斤拷头锟斤拷锟斤拷锟斤拷
    int y; //锟斤拷头锟斤拷锟斤拷锟斤拷
}snake;

//锟斤拷锟斤拷
struct Body
{
    int x; //锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
    int y; //锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
}body[ROW * COL]; //锟斤拷锟斤拷锟斤拷锟皆存储锟斤拷锟斤拷锟侥结构锟斤拷锟斤拷锟斤拷

int face[ROW][COL]; //锟斤拷锟斤拷锟较凤拷锟斤拷锟斤拷锟轿伙拷玫锟阶刺�

//锟斤拷锟截癸拷锟�
void HideCursor();
//锟斤拷锟斤拷锟阶�
void CursorJump(int x, int y);
//锟斤拷始锟斤拷锟斤拷锟斤拷
void InitInterface();
//锟斤拷色锟斤拷锟斤拷
void color(int c);
//锟斤拷锟侥硷拷锟斤拷取锟斤拷叻锟�
void ReadGrade();
//锟斤拷锟斤拷锟斤拷叻值锟斤拷募锟�
void WriteGrade();
//锟斤拷始锟斤拷锟斤拷
void InitSnake();
//锟斤拷锟斤拷锟斤拷锟绞筹拷锟�
void RandFood();
//锟叫断得凤拷锟斤拷锟斤拷锟�
void JudgeFunc(int x, int y);
//锟斤拷印锟斤拷锟诫覆锟斤拷锟斤拷
void DrawSnake(int flag);
//锟狡讹拷锟斤拷
void MoveSnake(int x, int y);
//执锟叫帮拷锟斤拷
void run(int x, int y);
//锟斤拷戏锟斤拷锟斤拷锟竭硷拷锟斤拷锟斤拷
void Game();

int max, grade; //全锟街憋拷锟斤拷
int main()
{
#pragma warning (disable:4996) //锟斤拷锟斤拷锟斤拷锟斤拷
    max = 0, grade = 0; //锟斤拷始锟斤拷锟斤拷锟斤拷
    system("title 贪锟斤拷锟斤拷"); //锟斤拷锟斤拷cmd锟斤拷锟节碉拷锟斤拷锟斤拷
    system("mode con cols=84 lines=23"); //锟斤拷锟斤拷cmd锟斤拷锟节的达拷小
    HideCursor(); //锟斤拷锟截癸拷锟�
    ReadGrade(); //锟斤拷锟侥硷拷锟斤拷取锟斤拷叻值锟絤ax锟斤拷锟斤拷
    InitInterface(); //锟斤拷始锟斤拷锟斤拷锟斤拷
    InitSnake(); //锟斤拷始锟斤拷锟斤拷
    srand((unsigned int)time(NULL)); //锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
    RandFood(); //锟斤拷锟斤拷锟斤拷锟绞筹拷锟�
    DrawSnake(1); //锟斤拷印锟斤拷
    Game(); //锟斤拷始锟斤拷戏
    return 0;
}

//锟斤拷锟截癸拷锟�
void HideCursor()
{
    CONSOLE_CURSOR_INFO curInfo; //锟斤拷锟斤拷锟斤拷锟斤拷息锟侥结构锟斤拷锟斤拷锟�
    curInfo.dwSize = 1; //锟斤拷锟矫伙拷锟街碉拷幕锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷效
    curInfo.bVisible = FALSE; //锟斤拷锟斤拷锟斤拷锟斤拷锟轿拷锟斤拷杉锟�
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); //锟斤拷取锟斤拷锟斤拷台锟斤拷锟�
    SetConsoleCursorInfo(handle, &curInfo); //锟斤拷锟矫癸拷锟斤拷锟较�
}
//锟斤拷锟斤拷锟阶�
void CursorJump(int x, int y)
{
    COORD pos; //锟斤拷锟斤拷锟斤拷位锟矫的结构锟斤拷锟斤拷锟�
    pos.X = x; //锟斤拷锟斤拷锟斤拷
    pos.Y = y; //锟斤拷锟斤拷锟斤拷
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); //锟斤拷取锟斤拷锟斤拷台锟斤拷锟�
    SetConsoleCursorPosition(handle, pos); //锟斤拷锟矫癸拷锟轿伙拷锟�
}
//锟斤拷始锟斤拷锟斤拷锟斤拷
void InitInterface()
{
    color(6); //锟斤拷色锟斤拷锟斤拷为锟斤拷锟斤拷色
    for (int i = 0; i < ROW; i++)
    {
        for (int j = 0; j < COL; j++)
        {
            if (j == 0 || j == COL - 1)
            {
                face[i][j] = WALL; //锟斤拷歉锟轿伙拷锟轿�
                CursorJump(2 * j, i);
                printf("锟斤拷");
            }
            else if (i == 0 || i == ROW - 1)
            {
                face[i][j] = WALL; //锟斤拷歉锟轿伙拷锟轿�
                printf("锟斤拷");
            }
            else
            {
                face[i][j] = KONG; //锟斤拷歉锟轿伙拷锟轿拷锟�
            }
        }
    }
    color(7); //锟斤拷色锟斤拷锟斤拷为锟斤拷色
    CursorJump(0, ROW);
    printf("锟斤拷前锟矫凤拷:%d", grade);
    CursorJump(COL, ROW);
    printf("锟斤拷史锟斤拷叩梅锟�:%d", max);
}
//锟斤拷色锟斤拷锟斤拷
void color(int c)
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c); //锟斤拷色锟斤拷锟斤拷
    //注锟斤拷SetConsoleTextAttribute锟斤拷一锟斤拷API锟斤拷应锟矫筹拷锟斤拷锟教接口ｏ拷
}
//锟斤拷锟侥硷拷锟斤拷取锟斤拷叻锟�
void ReadGrade()
{
    FILE* pf = fopen("贪锟斤拷锟斤拷锟斤拷叩梅旨锟铰�.txt", "r"); //锟斤拷只锟斤拷锟侥凤拷式锟斤拷锟侥硷拷
    if (pf == NULL) //锟斤拷锟侥硷拷失锟斤拷
    {
        pf = fopen("贪锟斤拷锟斤拷锟斤拷叩梅旨锟铰�.txt", "w"); //锟斤拷只写锟侥凤拷式锟斤拷锟侥硷拷
        fwrite(&max, sizeof(int), 1, pf); //锟斤拷max写锟斤拷锟侥硷拷锟斤拷锟斤拷时max为0锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷叩梅殖锟绞硷拷锟轿�0
    }
    fseek(pf, 0, SEEK_SET); //使锟侥硷拷指锟斤拷pf指锟斤拷锟侥硷拷锟斤拷头
    fread(&max, sizeof(int), 1, pf); //锟斤拷取锟侥硷拷锟斤拷锟叫碉拷锟斤拷叩梅值锟絤ax锟斤拷锟斤拷
    fclose(pf); //锟截憋拷锟侥硷拷
    pf = NULL; //锟侥硷拷指锟诫及时锟矫匡拷
}
//锟斤拷锟斤拷锟斤拷叻值锟斤拷募锟�
void WriteGrade()
{
    FILE* pf = fopen("贪锟斤拷锟斤拷锟斤拷叩梅旨锟铰�.txt", "w"); //锟斤拷只写锟侥凤拷式锟斤拷锟侥硷拷
    if (pf == NULL) //锟斤拷锟侥硷拷失锟斤拷
    {
        printf("锟斤拷锟斤拷锟斤拷叩梅旨锟铰际э拷锟絓n");
        exit(0);
    }
    fwrite(&grade, sizeof(int), 1, pf); //锟斤拷锟斤拷锟斤拷锟斤拷戏锟矫凤拷写锟斤拷锟侥硷拷锟斤拷锟斤拷
    fclose(pf); //锟截憋拷锟侥硷拷
    pf = NULL; //锟侥硷拷指锟诫及时锟矫匡拷
}
//锟斤拷始锟斤拷锟斤拷
void InitSnake()
{
    snake.len = 2; //锟竭碉拷锟斤拷锟藉长锟饺筹拷始锟斤拷为2
    snake.x = COL / 2; //锟斤拷头位锟矫的猴拷锟斤拷锟斤拷
    snake.y = ROW / 2; //锟斤拷头位锟矫碉拷锟斤拷锟斤拷锟斤拷
    //锟斤拷锟斤拷锟斤拷锟斤拷某锟绞硷拷锟�
    body[0].x = COL / 2 - 1;
    body[0].y = ROW / 2;
    body[1].x = COL / 2 - 2;
    body[1].y = ROW / 2;
    //锟斤拷锟斤拷头锟斤拷锟斤拷锟斤拷位锟矫斤拷锟叫憋拷锟�
    face[snake.y][snake.x] = HEAD;
    face[body[0].y][body[0].x] = BODY;
    face[body[1].y][body[1].x] = BODY;
}
//锟斤拷锟斤拷锟斤拷锟绞筹拷锟�
void RandFood()
{
    int i, j;
    do
    {
        //锟斤拷锟斤拷锟斤拷锟绞筹拷锟侥猴拷锟斤拷锟斤拷锟斤拷
        i = rand() % ROW;
        j = rand() % COL;
    } while (face[i][j] != KONG); //确锟斤拷锟斤拷锟斤拷食锟斤拷锟轿伙拷锟轿拷眨锟斤拷锟斤拷锟轿拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟�
    face[i][j] = FOOD; //锟斤拷食锟斤拷位锟矫斤拷锟叫憋拷锟�
    color(12); //锟斤拷色锟斤拷锟斤拷为锟斤拷色
    CursorJump(2 * j, i); //锟斤拷锟斤拷锟阶拷锟斤拷锟斤拷傻锟斤拷锟斤拷位锟矫达拷
    printf("锟斤拷"); //锟斤拷印食锟斤拷
}
//锟叫断得凤拷锟斤拷锟斤拷锟�
void JudgeFunc(int x, int y)
{
    //锟斤拷锟斤拷头锟斤拷锟斤拷锟斤拷锟斤拷锟轿伙拷锟斤拷锟绞筹拷铮拷锟矫凤拷
    if (face[snake.y + y][snake.x + x] == FOOD)
    {
        snake.len++; //锟斤拷锟斤拷锟接筹拷
        grade += 10; //锟斤拷锟铰碉拷前锟矫凤拷
        color(7); //锟斤拷色锟斤拷锟斤拷为锟斤拷色
        CursorJump(0, ROW);
        printf("锟斤拷前锟矫凤拷:%d", grade); //锟斤拷锟铰达拷印锟斤拷前锟矫凤拷
        RandFood(); //锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟绞筹拷锟�
    }
    //锟斤拷锟斤拷头锟斤拷锟斤拷锟斤拷锟斤拷锟轿伙拷锟斤拷锟角斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟较凤拷锟斤拷锟�
    else if (face[snake.y + y][snake.x + x] == WALL || face[snake.y + y][snake.x + x] == BODY)
    {
        Sleep(1000); //锟斤拷锟斤拷锟斤拷曳锟接κ憋拷锟�
        system("cls"); //锟斤拷锟斤拷锟侥�
        color(7); //锟斤拷色锟斤拷锟斤拷为锟斤拷色
        CursorJump(2 * (COL / 3), ROW / 2 - 3);
        if (grade > max)
        {
            printf("锟斤拷喜锟斤拷锟斤拷锟斤拷锟竭硷拷录锟斤拷锟斤拷呒锟铰硷拷锟斤拷锟轿�%d", grade);
            WriteGrade();
        }
        else if (grade == max)
        {
            printf("锟斤拷锟斤拷呒锟铰硷拷锟狡斤拷锟斤拷锟斤拷锟斤拷俅锟斤拷鸭锟�", grade);
        }
        else
        {
            printf("锟斤拷锟斤拷锟斤拷锟斤拷停锟斤拷锟角帮拷锟斤拷锟竭硷拷录锟斤拷锟�%d", max - grade);
        }
        CursorJump(2 * (COL / 3), ROW / 2);
        printf("GAME OVER");
        while (1) //询锟斤拷锟斤拷锟斤拷欠锟斤拷锟斤拷锟揭伙拷锟�
        {
            char ch;
            CursorJump(2 * (COL / 3), ROW / 2 + 3);
            printf("锟斤拷锟斤拷一锟斤拷?(y/n):");
            scanf("%c", &ch);
            if (ch == 'y' || ch == 'Y')
            {
                system("cls");
                main();
            }
            else if (ch == 'n' || ch == 'N')
            {
                CursorJump(2 * (COL / 3), ROW / 2 + 5);
                exit(0);
            }
            else
            {
                CursorJump(2 * (COL / 3), ROW / 2 + 5);
                printf("选锟斤拷锟斤拷锟斤拷锟斤拷俅锟窖★拷锟�");
            }
        }
    }
}
//锟斤拷印锟斤拷锟诫覆锟斤拷锟斤拷
void DrawSnake(int flag)
{
    if (flag == 1) //锟斤拷印锟斤拷
    {
        color(10); //锟斤拷色锟斤拷锟斤拷为锟斤拷色
        CursorJump(2 * snake.x, snake.y);
        printf("锟斤拷"); //锟斤拷印锟斤拷头
        for (int i = 0; i < snake.len; i++)
        {
            CursorJump(2 * body[i].x, body[i].y);
            printf("锟斤拷"); //锟斤拷印锟斤拷锟斤拷
        }
    }
    else //锟斤拷锟斤拷锟斤拷
    {
        if (body[snake.len - 1].x != 0) //锟斤拷止len++锟斤拷(0, 0)位锟矫碉拷墙锟斤拷锟斤拷
        {
            //锟斤拷锟斤拷尾锟斤拷锟斤拷为锟秸格即匡拷
            CursorJump(2 * body[snake.len - 1].x, body[snake.len - 1].y);
            printf("  ");
        }
    }
}
//锟狡讹拷锟斤拷
void MoveSnake(int x, int y)
{
    DrawSnake(0); //锟饺革拷锟角碉拷前锟斤拷锟斤拷示锟斤拷锟斤拷
    face[body[snake.len - 1].y][body[snake.len - 1].x] = KONG; //锟斤拷锟狡讹拷锟斤拷锟斤拷尾锟斤拷锟铰憋拷锟轿拷锟�
    face[snake.y][snake.x] = BODY; //锟斤拷锟狡讹拷锟斤拷锟斤拷头锟斤拷位锟矫憋拷为锟斤拷锟斤拷
    //锟斤拷锟狡讹拷锟斤拷锟斤拷锟斤拷锟斤拷锟轿伙拷锟斤拷锟斤拷锟斤拷锟揭拷锟斤拷锟�
    for (int i = snake.len - 1; i > 0; i--)
    {
        body[i].x = body[i - 1].x;
        body[i].y = body[i - 1].y;
    }
    //锟斤拷锟狡讹拷锟斤拷锟斤拷头位锟斤拷锟斤拷息锟斤拷为锟斤拷0锟斤拷锟斤拷锟斤拷锟斤拷位锟斤拷锟斤拷息
    body[0].x = snake.x;
    body[0].y = snake.y;
    //锟斤拷头锟斤拷位锟矫革拷锟斤拷
    snake.x = snake.x + x;
    snake.y = snake.y + y;
    DrawSnake(1); //锟斤拷印锟狡讹拷锟斤拷锟斤拷锟�
}
//执锟叫帮拷锟斤拷
void run(int x, int y)
{
    int t = 0;
    while (1)
    {
        if (t == 0)
            t = 3000; //锟斤拷锟斤拷t越小锟斤拷锟斤拷锟狡讹拷锟劫讹拷越锟届（锟斤拷锟皆革拷锟捷达拷锟斤拷锟斤拷锟斤拷戏锟窖度ｏ拷
        while (--t)
        {
            if (kbhit() != 0) //锟斤拷锟斤拷锟教憋拷锟矫伙拷锟斤拷锟斤拷锟剿筹拷循锟斤拷
                break;
        }
        if (t == 0) //锟斤拷锟斤拷未锟斤拷锟矫伙拷
        {
            JudgeFunc(x, y); //锟叫断碉拷锟斤拷锟轿伙拷煤锟斤拷欠锟矫凤拷锟斤拷锟斤拷戏锟斤拷锟斤拷
            MoveSnake(x, y); //锟狡讹拷锟斤拷
        }
        else //锟斤拷锟教憋拷锟矫伙拷
        {
            break; //锟斤拷锟斤拷Game锟斤拷锟斤拷锟斤拷取锟斤拷值
        }
    }
}
//锟斤拷戏锟斤拷锟斤拷锟竭硷拷锟斤拷锟斤拷
void Game()
{
    int n = RIGHT; //锟斤拷始锟斤拷戏时锟斤拷默锟斤拷锟斤拷锟斤拷贫锟�
    int tmp = 0; //锟斤拷录锟竭碉拷锟狡讹拷锟斤拷锟斤拷
    goto first; //锟斤拷一锟轿斤拷锟斤拷循锟斤拷锟斤拷锟斤拷默锟较凤拷锟斤拷前锟斤拷
    while (1)
    {
        n = getch(); //锟斤拷取锟斤拷值
        //锟斤拷执锟斤拷前锟斤拷锟斤拷要锟斤拷锟斤拷锟斤拷取锟侥帮拷锟斤拷锟斤拷锟叫碉拷锟斤拷
        switch (n)
        {
        case UP:
        case DOWN: //锟斤拷锟斤拷没锟斤拷锟斤拷恰锟斤拷稀锟斤拷锟斤拷隆锟�
            if (tmp != LEFT && tmp != RIGHT) //锟斤拷锟斤拷锟斤拷一锟斤拷锟竭碉拷锟狡讹拷锟斤拷锟斤拷锟角★拷锟襟”伙拷锟揭★拷
            {
                n = tmp; //锟斤拷么锟斤拷一锟斤拷锟竭碉拷锟狡讹拷锟斤拷锟斤拷锟斤拷锟斤拷为锟斤拷一锟斤拷锟竭碉拷锟狡讹拷锟斤拷锟斤拷
            }
            break;
        case LEFT:
        case RIGHT: //锟斤拷锟斤拷没锟斤拷锟斤拷恰锟斤拷蟆被锟斤拷摇锟�
            if (tmp != UP && tmp != DOWN) //锟斤拷锟斤拷锟斤拷一锟斤拷锟竭碉拷锟狡讹拷锟斤拷锟斤拷锟角★拷锟较★拷锟斤拷锟铰★拷
            {
                n = tmp; //锟斤拷么锟斤拷一锟斤拷锟竭碉拷锟狡讹拷锟斤拷锟斤拷锟斤拷锟斤拷为锟斤拷一锟斤拷锟竭碉拷锟狡讹拷锟斤拷锟斤拷
            }
        case SPACE:
        case ESC:
        case 'r':
        case 'R':
            break; //锟斤拷锟侥革拷锟斤拷锟斤拷锟斤拷锟�
        default:
            n = tmp; //锟斤拷锟斤拷锟斤拷锟斤拷效锟斤拷默锟斤拷为锟斤拷一锟斤拷锟斤拷锟狡讹拷锟侥凤拷锟斤拷
            break;
        }
    first: //锟斤拷一锟轿斤拷锟斤拷循锟斤拷锟斤拷锟斤拷默锟较凤拷锟斤拷前锟斤拷
        switch (n)
        {
        case UP: //锟斤拷锟斤拷锟斤拷锟斤拷锟�
            run(0, -1); //锟斤拷锟斤拷锟狡讹拷锟斤拷锟斤拷锟斤拷锟斤拷偏锟斤拷为0锟斤拷锟斤拷锟斤拷锟斤拷偏锟斤拷为-1锟斤拷
            tmp = UP; //锟斤拷录锟斤拷前锟竭碉拷锟狡讹拷锟斤拷锟斤拷
            break;
        case DOWN: //锟斤拷锟斤拷锟斤拷锟斤拷锟�
            run(0, 1); //锟斤拷锟斤拷锟狡讹拷锟斤拷锟斤拷锟斤拷锟斤拷偏锟斤拷为0锟斤拷锟斤拷锟斤拷锟斤拷偏锟斤拷为1锟斤拷
            tmp = DOWN; //锟斤拷录锟斤拷前锟竭碉拷锟狡讹拷锟斤拷锟斤拷
            break;
        case LEFT: //锟斤拷锟斤拷锟斤拷锟斤拷锟�
            run(-1, 0); //锟斤拷锟斤拷锟狡讹拷锟斤拷锟斤拷锟斤拷锟斤拷偏锟斤拷为-1锟斤拷锟斤拷锟斤拷锟斤拷偏锟斤拷为0锟斤拷
            tmp = LEFT; //锟斤拷录锟斤拷前锟竭碉拷锟狡讹拷锟斤拷锟斤拷
            break;
        case RIGHT: //锟斤拷锟斤拷锟斤拷锟斤拷锟�
            run(1, 0); //锟斤拷锟斤拷锟狡讹拷锟斤拷锟斤拷锟斤拷锟斤拷偏锟斤拷为1锟斤拷锟斤拷锟斤拷锟斤拷偏锟斤拷为0锟斤拷
            tmp = RIGHT; //锟斤拷录锟斤拷前锟竭碉拷锟狡讹拷锟斤拷锟斤拷
            break;
        case SPACE: //锟斤拷停
            system("pause>nul"); //锟斤拷停锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟�
            break;
        case ESC: //锟剿筹拷
            system("cls"); //锟斤拷锟斤拷锟侥�
            color(7); //锟斤拷色锟斤拷锟斤拷为锟斤拷色
            CursorJump(COL - 8, ROW / 2);
            printf("  锟斤拷戏锟斤拷锟斤拷  ");
            CursorJump(COL - 8, ROW / 2 + 2);
            exit(0);
        case 'r':
        case 'R': //锟斤拷锟铰匡拷始
            system("cls"); //锟斤拷锟斤拷锟侥�
            main(); //锟斤拷锟斤拷执锟斤拷锟斤拷锟斤拷锟斤拷
        }
    }
}
